package com.tbit.uqbike.service.event.listener;

import com.tbit.config.tg.TbitAdminAccountInfo;
import com.tbit.uqbike.service.event.EventType;
import com.tbit.uqbike.service.event.eventItem.AccountEvent;
import com.tbit.uqbike.service.event.handler.EventHandler;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * 品牌创建 / 修改 / 删除兼听
 *
 * @author <a href="mailto:zhang.zhiliang@tbit.com.cn">Zhang ZhiLiang</a>
 * @version 1.0
 * @see <a href="http://adsl1.tbit.com.cn:21490/pages/viewpage.action?pageId=82182491">订单号规范</a>
 * @since 2025/5/27
 */
@Slf4j
@Component
@AllArgsConstructor
public class AccountListener {

    private final List<EventHandler> eventHandlers;

    @EventListener
    public void listener(AccountEvent accountEvent) {
        TbitAdminAccountInfo accountInfo = (TbitAdminAccountInfo) accountEvent.getSource();
        Assert.notNull(accountInfo,"accountInfo must not be empty");
        if (CollectionUtils.isEmpty(eventHandlers)) {
            return;
        }
        EventType eventType = accountEvent.getEventType();
        eventHandlers.forEach(x -> x.handle(accountInfo,eventType));
    }

}
